package com.ljph.ljaker.validator;

import com.jfinal.kit.StrKit;
import com.ljph.jfinal.base.BaseValidator;
import com.ljph.ljaker.model.User;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * Created by yuzhou on 16/9/25.
 */
public class UserLoginValidator extends BaseValidator {

    @Override
    protected void validate() {
        String username = getPara("username");
        if(StrKit.isBlank(username)) {
            addError("用户名没有提供");
        }

        String password = getPara("password");
        if(StrKit.isBlank(password)) {
            addError("密码没有提供");
        }

        User user = User.dao.findByUsername(username);
        if(user == null) {
            addError("用户名不存在");
        }

        String expectPassword = DigestUtils.md5Hex(DigestUtils.md5Hex(password)) + user.getSalt();
        if(!expectPassword.equalsIgnoreCase(user.getPassword())) {
            addError("密码不正确");
        }

        setAttr("user", user);
    }
}
